package com.kzd.gtgame.main.home

import android.content.Intent
import androidx.lifecycle.LifecycleOwner
import com.kzd.gtgame.R
import com.kzd.gtgame.custom.setOnClickAntiShakeListener
import com.kzd.gtgame.entity.BargainDetails
import com.kzd.gtgame.jetpack.vm.BargainViewModel
import com.kzd.gtgame.main.*
import com.kzd.gtgame.main.home.adapter.BargainImageAdapter
import com.module.lemlin.http.Status
import com.module.lemlin.http.msg
import com.module.lemlin.image.loadImage
import com.module.lemlin.owner.OwnerViewModeActivity
import kotlinx.android.synthetic.main.activity_bargain_details.*
import java.lang.StringBuilder

class BargainDetailsActivity : OwnerViewModeActivity<BargainViewModel>() {

    private val mBargainImageAdapter = BargainImageAdapter()

    private val mBargainId: String
        get() = intent.extras?.getString(KEY_BARGAIN_DETAILS_ID) ?: ""

    override fun viewModel(): BargainViewModel {
        return createViewModel()
    }

    override val layoutResId: Int
        get() = R.layout.activity_bargain_details

    override fun stateBar(): StatusBar {
        return StatusBar(color = R.color.white, view = tlBargainDetailsBar)
    }

    override fun initView() {

        rvBargainDetailsImageBody.adapter = mBargainImageAdapter

        tvBargainDetailsCollect.setOnClickAntiShakeListener {
            val isCollect = viewModel.bargainDetailsLiveData.value
                ?.data?.data?.shoucang ?: 0
            if (isCollect == 0) checkLogin {
                viewModel.userCollectAdd(mBargainId, type = 2)
            } else checkLogin {
                viewModel.userCollectDel(mBargainId, type = 2)
            }
        }

        tvGameListDownload.setOnClickAntiShakeListener {
            val gameId = viewModel.bargainDetailsLiveData.value?.data?.data?.gid
                ?: return@setOnClickAntiShakeListener
            toGameDetailsActivity(gameId)
        }

        tvBargainDetailsSubmit.setOnClickAntiShakeListener {
            val data = viewModel.bargainDetailsLiveData.value?.data?.data
                ?: return@setOnClickAntiShakeListener
            if (data.status == "1" && data.shop_ok == "0") {
                toBargainPaymentActivity(data)
            } else if (data.status == "2") {
                toBargainSellActivity(data)
            }
        }
    }

    override fun initData() {
        viewModel.bargainDetails(mBargainId)
    }

    override fun initViewMode() {
        viewModel.bargainDetailsLiveData.observe(this as LifecycleOwner, { response ->
            mslMultiStatusLayout.setLoadingStatus(response)
            if (response.status == Status.FAILURE) {
                toast(response.error?.msg)
            }
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data
                ?: return@observe
            fillData(data)
        })

        viewModel.userCollectAddLiveData.observe(this as LifecycleOwner, { response ->
            if (response.status == Status.FAILURE) {
                toast(response.error?.msg)
            }
            if (response.status != Status.SUCCESS)
                return@observe
            viewModel.bargainDetailsLiveData.value?.data?.data?.apply {
                this.shoucang = 1
            }
            toast(response.data?.message)
        })

        viewModel.userCollectDelLiveData.observe(this as LifecycleOwner, { response ->
            if (response.status == Status.FAILURE) {
                toast(response.error?.msg)
            }
            if (response.status != Status.SUCCESS)
                return@observe
            viewModel.bargainDetailsLiveData.value?.data?.data?.apply {
                this.shoucang = 0
            }
            toast(response.data?.message)
        })
    }

    private fun fillData(bd: BargainDetails) {
        ivBargainDetailsIcon.loadImage(bd.icon)
        tvBargainDetailsName.text = bd.gname
        tvBargainDetailsPrice.text = StringBuilder()
            .append("￥").append(bd.money)
        tvBargainDetailsType.text = StringBuilder()
            .append(bd.type).append(" · ").append(bd.number)
            .append("次下载 · ").append(bd.size).append("MB")
        tvBargainDetailsTime.text = bd.create_time
        tvBargainDetailsSmall.text = bd.role_name
        tvBargainDetailsService.text = bd.sname
        tvBargainDetailsStatus.text = StringBuilder()
            .append("此号已创建").append(bd.regday).append("天，实际累充：")
            .append(bd.pay_money).append("元")
        tvBargainDetailsIntroduction.text = bd.name
        tvBargainDetailsContent.text = bd.introduction

        mBargainImageAdapter.setNewInstance(bd.images)

        ivBargainDetailsStatus.apply {
            if (bd.status == "0" && bd.shop_ok == "0") {
                setImageResource(R.mipmap.trade_checking_icon)
            } else if (bd.status == "1" && bd.shop_ok == "0") {
                setImageResource(R.mipmap.trade_checked_icon)
            } else if (bd.status == "1" && bd.shop_ok == "1") {
                setImageResource(R.mipmap.trade_finish_icon)
            } else if (bd.status == "2") {
                setImageResource(R.mipmap.trade_rejected_icon)
            } else {
                setImageResource(0)
            }
        }

        tvBargainDetailsSubmit.apply {
            text = if (bd.status == "0" && bd.shop_ok == "0") {
                "待审核"
            } else if (bd.status == "1" && bd.shop_ok == "0") {
                "立即购买"
            } else if (bd.status == "1" && bd.shop_ok == "1") {
                "已出售"
            } else if (bd.status == "2") {
                "重新出售"
            } else {
                "立即购买"
            }
        }.isEnabled = if (bd.status == "0" && bd.shop_ok == "0") {
            false
        } else if (bd.status == "1" && bd.shop_ok == "0") {
            true
        } else if (bd.status == "1" && bd.shop_ok == "1") {
            false
        } else if (bd.status == "2") true else true
    }

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        initData()
    }

}